package com.mypaper.list;

import com.mypaper.elem.ScheduleElement;
import com.mypaper.elem.ScheduleNode;

public class ScheduleResults{
/*
 * 一系列的调度结点
 * 每个结点包括：任务序号，任务所在处理器，任务开始时间，任务结束时间
 */
	public ScheduleNode first;
	public ScheduleNode last;
	public int count;
	
	public ScheduleResults(){
		first=null;
		last=null;
		count=0;
	}
	
	public boolean isfindTask(int taskSN){
		return !(findTask(taskSN)==null);
	}
	
	public ScheduleElement findTask(int taskSN){
		ScheduleNode currentNode=first;
		while(currentNode!=null){
			if(currentNode.info.taskID==taskSN)
				return currentNode.info.getCopy();
			currentNode=currentNode.next;
		}
		return null;
	}
	
	public void getCopy(ScheduleResults otherResults){
		ScheduleNode newNode;
		ScheduleNode current;
		first=null;
		if(otherResults.first==null){
			first=null;
			last=null;
			count=0;
		}else{
			count=otherResults.count;
			current=otherResults.first;
			first=new ScheduleNode();
			first.info=current.info.getCopy();
			first.next=null;
			last=first;
			current=current.next;
			
			while(current!=null){	
				newNode=new ScheduleNode();
				newNode.info=current.info.getCopy();
				newNode.next=null;
				
				last.next=newNode;
				last=newNode;
				current=current.next;
			}
			newNode=null;
		}
	}
	
	public void copyList(ScheduleResults otherResults){
		if(this!=otherResults){
			getCopy(otherResults);
		}
	}
	public void insertNode(ScheduleElement insertItem){
		ScheduleNode newNode;
		
		newNode = new ScheduleNode();
		newNode.info=insertItem.getCopy();
		newNode.next=null;
		
		if(last==null){
			first=newNode;
			last=newNode;
			count++;
		}else{
			newNode.next=first;
			first=newNode;
			count++;
		}
	}	
	
	public void deleteNode(ScheduleElement deleteItem){
		ScheduleNode current;
		ScheduleNode trailCurrent;
		boolean found;
		if(first==null){
			System.out.print("The list is empty");
		}else{
			current=first;
			trailCurrent=first;
			if(current.info.equals(deleteItem)){
				first=first.next;
				if(first==null){
					last=null;
				}
				count--;
			}else{
				found=false;
				while(current!=null&&!found){
					if(current.info.equals(deleteItem)){
						found=true;
					}else{
						trailCurrent=current;
						current=current.next;
					}
				}
				if(current==null)
					System.out.println("The item to be deleted is not in the list.");					
				else{
					trailCurrent.next=current.next;
					if(current.equals(last)){
						last=trailCurrent;
					}
					count--;
				}
			}
		}
		
		
	}
	
	public ScheduleElement deleteNode(int taskSN){
		ScheduleElement deletedNode=null;
		ScheduleNode current;
		ScheduleNode trailCurrent;
		boolean found;
		if(first==null){
			System.out.print("The list is empty");
		}else{
			current=first;
			trailCurrent=first;
			if(current.info.taskID==taskSN){
				deletedNode=current.info;
				first=first.next;
				if(first==null){
					last=null;
				}
				count--;
			}else{
				found=false;
				while(current!=null&&!found){
					if(current.info.taskID==taskSN){
						deletedNode=current.info;
						found=true;
					}else{
						trailCurrent=current;
						current=current.next;
					}
				}
				if(current==null)
					System.out.println("The item to be deleted is not in the list.");					
				else if(current.next==null){
					trailCurrent.next=current.next;
					last=trailCurrent;
					count--;
				}else{
					trailCurrent.next=current.next;
					count--;
				}
			}
		}
		return deletedNode;
	}
	
	public void print(){
		ScheduleNode current;
		current=first;
		System.out.println("TaskID\tProc\tStartT\tEndT");
		while(current!=null){
			System.out.println(current.info.taskID+"\t"+current.info.processorID+"\t"
					+current.info.startTime+"\t"+current.info.endTime);
			current=current.next;
		}
	}

	public void updateNode(ScheduleElement tempElement, float alst, float alft) {
		// TODO Auto-generated method stub
		ScheduleNode currentNode=first;
		while(currentNode!=null){
			if(currentNode.info.equals(tempElement)){
				currentNode.info.startTime=alst;
				currentNode.info.endTime=alft;
				break;
			}
			currentNode=currentNode.next;
		}
	}

}
